package com.ag.hmk.action;

import javax.annotation.Resource;

import com.ag.bean.TbLaw;
import com.ag.bean.TbLawnature;
import com.ag.bean.TbLkw;
import com.ag.bean.TbType;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.hmk.service.HmkUiltService;
import com.ag.hmk.service.LawNatureService;
import com.ag.hmk.service.LawService;
import com.ag.hmk.service.LawkwService;
import com.ag.hmk.service.WukuTypeService;
/**
 * 
 * @author 黄木看
 * 保存法规action
 *
 */
public class SaveLawAction extends BaseAction {

	private static final long serialVersionUID = 1714149315813458816L;
	private LawService lawServImpl;
	private WukuTypeService wukuTypeServImpl;
	private LawNatureService lawNatureServImpl;
	private LawkwService lawkwServImpl;
	private HmkUiltService hmkUiltServImpl;
	@Resource(name = "iDServiceImpl")
	private IIDService idservice;
	
	private String lawID1;
	private String typeID1;
	private String lawNature1;
	private String lawName1;
	private String lawDetail1;
	private String fileID1;
	private String fileNameID1;
	
	private String kwStr1;
	private String ajaxRST;
	
	
	
	/*
	 * 保存法规
	 */
	public String saveLaw() throws Exception{
		this.setAjaxRST("0");
		try {
			TbType tbType = wukuTypeServImpl.findTypeByID(typeID1);
			TbLawnature tbLawnature = lawNatureServImpl.findByID(lawNature1);
			if (tbType!=null && tbLawnature!=null) {
				if (lawName1!=null && lawName1.length()>0) {
					TbLaw law = new TbLaw();
					String lawID = idservice.findTablePK(IDUtil.TB_LAW);
					law.setLawId(lawID);
					law.setTbType(tbType);
					law.setTbLawnature(tbLawnature);
					law.setLawName(lawName1);
					law.setLawDetail(lawDetail1);
					law.setLawFjName(fileID1);
					law.setLawFjRote(fileNameID1);
					
					lawServImpl.save(law);  //保存法规
					
					//下面保存关键字
					if (kwStr1!=null && kwStr1.length()>0) {
						String [] str1 = kwStr1.split(",");
						for (int i = 0; i < str1.length; i++) {
							System.out.println("---keyword:"+str1[i]);
							TbLkw lkw1 = new TbLkw();
							String lwkID = idservice.findTablePK(IDUtil.TB_LKW);
							lkw1.setLawKwid(lwkID);
							lkw1.setLawKw(str1[i]);
							lkw1.setTbLaw(law);
							
							lawkwServImpl.save(lkw1);  //保存关键字
							System.out.println("---保存一个关键字成功");
						}
					} else {
						System.out.println("----没有关键字");
					}
					//保存关键字结束
					this.setAjaxRST("1");
					System.out.println("---save law ok");
					
				} else {
					System.out.println("law name and so on can not to be null");
				}
			} else {
				System.out.println("type can not to be null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		return "saveLawOK";
	}

	

	/*
	 * 更新法规
	 */
	public String updateLaw() throws Exception{
		this.setAjaxRST("0");
		try {
			TbLaw law = lawServImpl.findByID(lawID1);
			TbType tbType = wukuTypeServImpl.findTypeByID(typeID1);
			TbLawnature tbLawnature = lawNatureServImpl.findByID(lawNature1);
			if (tbType!=null && tbLawnature!=null) {
				if (law!=null && lawName1!=null && lawName1.length()>0) {
					law.setTbType(tbType);
					law.setTbLawnature(tbLawnature);
					law.setLawName(lawName1);
					law.setLawDetail(lawDetail1);
					law.setLawFjName(fileID1);
					law.setLawFjRote(fileNameID1);
					
					lawServImpl.save(law);  //保存法规
					
					//删除关键字
					String sql = "DELETE FROM TB_LKW WHERE LAW_ID="+lawID1;
					hmkUiltServImpl.queryUpdate(sql);
					
					//下面保存关键字
					if (kwStr1!=null && kwStr1.length()>0) {
						String [] str1 = kwStr1.split(",");
						for (int i = 0; i < str1.length; i++) {
							System.out.println("---keyword:"+str1[i]);
							TbLkw lkw1 = new TbLkw();
							String lwkID = idservice.findTablePK(IDUtil.TB_LKW);
							lkw1.setLawKwid(lwkID);
							lkw1.setLawKw(str1[i]);
							lkw1.setTbLaw(law);
							
							lawkwServImpl.save(lkw1);  //保存关键字
							
							System.out.println("---保存一个关键字成功");
						}
					} else {
						System.out.println("----没有关键字");
					}
					//保存关键字结束
					this.setAjaxRST("1");
					System.out.println("---save law ok");
					
				} else {
					System.out.println("law name and so on can not to be null");
				}
			} else {
				System.out.println("type can not to be null");
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.setAjaxRST("0");
		}
		
		return "saveLawOK";
	}





	public void setTypeID1(String typeID1) {
		this.typeID1 = typeID1;
	}




	public void setLawNature1(String lawNature1) {
		this.lawNature1 = lawNature1;
	}




	public void setLawName1(String lawName1) {
		this.lawName1 = lawName1;
	}




	public void setLawDetail1(String lawDetail1) {
		this.lawDetail1 = lawDetail1;
	}




	public String getAjaxRST() {
		return ajaxRST;
	}




	public void setAjaxRST(String ajaxRST) {
		this.ajaxRST = ajaxRST;
	}





	public void setKwStr1(String kwStr1) {
		this.kwStr1 = kwStr1;
	}




	public void setFileID1(String fileID1) {
		this.fileID1 = fileID1;
	}




	public void setFileNameID1(String fileNameID1) {
		this.fileNameID1 = fileNameID1;
	}




	public void setLawID1(String lawID1) {
		this.lawID1 = lawID1;
	}



	public void setLawServImpl(LawService lawServImpl) {
		this.lawServImpl = lawServImpl;
	}



	public void setWukuTypeServImpl(WukuTypeService wukuTypeServImpl) {
		this.wukuTypeServImpl = wukuTypeServImpl;
	}



	public void setLawNatureServImpl(LawNatureService lawNatureServImpl) {
		this.lawNatureServImpl = lawNatureServImpl;
	}



	public void setLawkwServImpl(LawkwService lawkwServImpl) {
		this.lawkwServImpl = lawkwServImpl;
	}



	public void setHmkUiltServImpl(HmkUiltService hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}

}
